package com.iotimc.attendancesystem.attendance;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.base.BaseFragment;
import com.iotimc.attendancesystem.dao.BasicSettingEntityDao;
import com.iotimc.attendancesystem.dao.GreenDaoManager;
import com.iotimc.attendancesystem.entity.BasicSettingEntity;
import com.iotimc.attendancesystem.manage.ManageActivity;
import com.iotimc.attendancesystem.util.TtsUtil;
import com.iotimc.attendancesystem.widget.InputNumLayout;

import butterknife.BindView;

/**
 * @author cwj
 * @date 2017/11/28
 */

public class LoginFragment extends BaseFragment {
    @BindView(R.id.et_login_password)
    EditText etPassword;
    @BindView(R.id.bt_login_check)
    Button btCheck;
    private String password;
    @BindView(R.id.input_num_layout)
    InputNumLayout inputNumLayout;

    @Override
    protected int getResId() {
        return R.layout.fragment_login;
    }

    @Override
    public void onResume() {
        super.onResume();
        BasicSettingEntityDao settingEntityDao = GreenDaoManager.getInstance().getSession().getBasicSettingEntityDao();
        BasicSettingEntity entity = settingEntityDao.load(1L);
        password = entity.getPassword();

        if (etPassword != null) {
            etPassword.setText("");
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = String.valueOf(LoginFragment.this.etPassword.getText());
                if (password != null && password.equals(text)) {
                    TtsUtil.ttsSpeek2("密码验证成功");
                    ((SystemActivity) getActivity()).stopTime();
                    Intent intent = new Intent(getActivity(), ManageActivity.class);
                    intent.putExtra("admin", false);
                    LoginFragment.this.etPassword.setText("");
                    startActivityForResult(intent, 9527);
                    ((SystemActivity) getActivity()).showMainFragment();
                } else {
                    TtsUtil.ttsSpeek2("密码验证失败");
                }
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        etPassword.setText("");
        super.onHiddenChanged(hidden);
    }

    @Override
    protected void initView(View view) {
        inputNumLayout.setEditText(etPassword);
    }
}
